package com.dp.struts2.yc.message;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletContext;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.util.ServletContextAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.dp.pojo.CommentContent;
import com.dp.pojo.PageCtrl;
import com.dp.pub.BasePageAction;
import com.dp.pub.SysProperty;
import com.dp.service.jdc.ChannelServiceImpl3;
import com.dp.service.jdc.IndexServiceImpl;
import com.dp.service.jdc.MessageServiceImpl;
import com.dp.vo.TChannels;
/**
 * @author Administrator
 *
 */
@Results({
    @Result(name="messageList",type="redirectAction",location="message!messageindex.do")
})

@Controller
@Scope("session")
public class MessageAction extends BasePageAction implements ServletContextAware {
	
	private static Logger logger = Logger.getLogger(MessageAction.class);
	
	private List<TChannels> navChannels;
	private List<List<TChannels>> nav2Channels;
	private CommentContent commentcontent2 ;
    private List<CommentContent> commentcontent1;
    private List<CommentContent> commentReply;
    
	
    //姓名
	private String clientid;
	//email
	private String deleter;
	//电话
	private String auditer;
	//标题
	private String name;
	
	/**
	 * 内容
	 */
	private String commenttext;
	
	private PageCtrl pageCtrl;
    private int pageSize = 5;
    private int pageNo = 0;
	@SuppressWarnings("unchecked")
	@Autowired
	private IndexServiceImpl indexService;
	 @Autowired
	private ChannelServiceImpl3 channelService;
	public String messageindex(){
		
		//获得留言版导航
		navChannels = indexService.getChannelsList(SysProperty.getPrep("hdnews_rootch"));
		nav2Channels = new ArrayList<List<TChannels>>();
        for (TChannels t : navChannels) {
            List<TChannels> temp = channelService.getLastChannels(t.getChannelId());
            nav2Channels.add(temp);
        }
		commentcontent1= messageservice.getMessage(pageNo,pageSize);
		commentReply = new ArrayList<CommentContent>();
		for (CommentContent comment : commentcontent1) {
		    if(messageservice.getMessageReply(comment.getCid())!=null){
		        
		        commentReply.add(messageservice.getMessageReply(comment.getCid()));
		    }
		    
        }
		pageCtrl = messageservice.getPageCtrl(pageNo, pageSize);
		return "messageindex";
	}
	@Override
	public void setServletContext(ServletContext arg0) {
		// TODO Auto-generated method stub
		
	}
	@Autowired
	private MessageServiceImpl messageservice;
    public String addmessage(){
        navChannels = indexService.getChannelsList(SysProperty.getPrep("hdnews_rootch"));
        for (TChannels t : navChannels) {
            nav2Channels = new ArrayList<List<TChannels>>();
            List<TChannels> temp = channelService.getLastChannels(t.getChannelId());
            nav2Channels.add(temp);
        }
        
        commentcontent2 = new CommentContent();
        
        Date rightNow = new Date();
        DateFormat format1 = new SimpleDateFormat("MMddHHmmss");
        DateFormat format2 = new SimpleDateFormat("yy/MM/dd HH:mm:ss");
        commentcontent2.setCid(Integer.parseInt(format1.format(rightNow)));
        commentcontent2.setCommenttime(format2.format(rightNow));
        commentcontent2.setChannleid(1);
        commentcontent2.setClientid(clientid);
        commentcontent2.setDeleter(deleter);
        commentcontent2.setAuditer(auditer);
        commentcontent2.setName(name);
        commentcontent2.setCommenttext(commenttext);
        messageservice.addMessage(commentcontent2);
        
        pageCtrl = messageservice.getPageCtrl(pageNo, pageSize);
        commentcontent1 = messageservice.getMessage(pageNo,pageSize);
    	
    	return "messageList";
    }
    
    
    public String getCommenttext() {
        return commenttext;
    }
    public void setCommenttext(String commenttext) {
        this.commenttext = commenttext;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getClientid() {
        return clientid;
    }
    public void setClientid(String clientid) {
        this.clientid = clientid;
    }
    public String getDeleter() {
        return deleter;
    }
    public void setDeleter(String deleter) {
        this.deleter = deleter;
    }
    public String getAuditer() {
        return auditer;
    }
    public void setAuditer(String auditer) {
        this.auditer = auditer;
    }
    
    
    public List<CommentContent> getCommentcontent1() {
        return commentcontent1;
    }
    public void setCommentcontent1(List<CommentContent> commentcontent1) {
        this.commentcontent1 = commentcontent1;
    }
    public CommentContent getCommentcontent2() {
        return commentcontent2;
    }
    public void setCommentcontent2(CommentContent commentcontent2) {
        this.commentcontent2 = commentcontent2;
    }
    public List<TChannels> getNavChannels() {
        return navChannels;
    }
    public void setNavChannels(List<TChannels> navChannels) {
        this.navChannels = navChannels;
    }
    public PageCtrl getPageCtrl() {
        return pageCtrl;
    }
    public void setPageCtrl(PageCtrl pageCtrl) {
        this.pageCtrl = pageCtrl;
    }
    public int getPageSize() {
        return pageSize;
    }
    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }
    public int getPageNo() {
        return pageNo;
    }
    public void setPageNo(int pageNo) {
        this.pageNo = pageNo;
    }
    public List<CommentContent> getCommentReply() {
        return commentReply;
    }
    public void setCommentReply(List<CommentContent> commentReply) {
        this.commentReply = commentReply;
    }
    public List<List<TChannels>> getNav2Channels() {
        return nav2Channels;
    }
    public void setNav2Channels(List<List<TChannels>> nav2Channels) {
        this.nav2Channels = nav2Channels;
    }
}
